package com.dg.river.core.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;

import com.dg.river.R;
import com.dg.river.core.util.TextUtil;

/**
 * Created by hy on 2018/10/16
 * Description : 标准含标题dialog
 */
public class NormalMessageDialog extends Dialog {

    private static TextView tv_confirm;
    private static TextView tv_cancel;
    private static TextView tv_title;
    private static TextView tv_message;

    private static NormalMessageDialog dialog;

    public NormalMessageDialog(Context context) {
        super(context);
    }

    public NormalMessageDialog(Context context, int theme) {
        super(context, theme);
    }

    public static class Builder {
        private Context context;
        private String title;
        private String message;
        private String confirm_btnText;
        private int confirm_btnColor;
        private String cancel_btnText;
        private int cancel_btnColor;
        private boolean cancel = false;
        private OnClickListener confirm_btnClickListener;
        private OnClickListener cancel_btnClickListener;

        public Builder(Context context) {
            this.context = context;
        }

        /**
         * 设置是否能够取消
         *
         * @param bool
         * @return
         */
        public Builder setCancel(boolean bool) {
            this.cancel = bool;
            return this;
        }

        /**
         * Set the Dialog title from String
         *
         * @param title
         * @return
         */
        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        /**
         * Set the Dialog message from String
         *
         * @param message
         * @return
         */
        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        /**
         * Set the positive button and it's listener
         *
         * @param confirm_btnText
         * @return
         */
        public Builder setConfirmButton(String confirm_btnText, int confirm_btnColor, OnClickListener listener) {
            this.confirm_btnText = confirm_btnText;
            this.confirm_btnColor = confirm_btnColor;
            this.confirm_btnClickListener = listener;
            return this;
        }

        /**
         * Set the negative button and it's listener
         *
         * @param
         * @return
         */
        public Builder setCancelButton(String cancel_btnText, int cancel_btnColor, OnClickListener listener) {
            this.cancel_btnText = cancel_btnText;
            this.cancel_btnColor = cancel_btnColor;
            this.cancel_btnClickListener = listener;
            return this;
        }

        /**
         * 传入0为默认样式 否则为自定义样式
         *
         * @param style
         * @return
         */
        public NormalMessageDialog create(int style) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            if (style == 0) {
                dialog = new NormalMessageDialog(context, R.style.dialog_message_normal);
            } else {
                dialog = new NormalMessageDialog(context, style);
            }
            View layout = inflater.inflate(R.layout.dialog_normal_message, null);
            dialog.addContentView(layout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            dialog.getWindow().setWindowAnimations(R.style.dialogWindowAnim);

            tv_confirm = layout.findViewById(R.id.tv_confirm);
            tv_cancel = layout.findViewById(R.id.tv_cancel);
            tv_title = layout.findViewById(R.id.tv_title);
            tv_message = layout.findViewById(R.id.tv_message);
            /**
             * 设置显示
             */
            tv_title.setText(TextUtil.isEmptyConvert(title));
            tv_message.setText(TextUtil.isEmptyConvert(message));
            tv_confirm.setText(confirm_btnText);
            tv_cancel.setText(cancel_btnText);
            if (cancel_btnColor != -1) {
                tv_cancel.setTextColor(context.getResources().getColor(cancel_btnColor));
            }
            if (confirm_btnColor != -1) {
                tv_confirm.setTextColor(context.getResources().getColor(confirm_btnColor));
            }
            tv_confirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (confirm_btnClickListener != null) {
                        confirm_btnClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                    }
                }
            });
            tv_cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (cancel_btnClickListener != null) {
                        cancel_btnClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
                    }
                }
            });
            dialog.setContentView(layout);
            if (!cancel) {
                dialog.setCancelable(false);
            } else {
                dialog.setCancelable(true);
            }
            return dialog;
        }
    }
}
